from enum import Enum
import numpy as np

class TokenType(Enum):
    # 常数
    CONST_ID = 'CONST_ID'
    # 唯一参数T
    T = 'T'
    # 支持函数：SIN COS TAN SQRT EXP LN
    FUNC = 'FUNCTION'
    # 保留关键字
    ## FOR T FROM 起点 TO 终点 STEP 步长 DRAW(x,y);
    FOR = 'FOR'
    FROM = 'FROM'
    TO = 'TO'
    STEP = 'STEP'
    DRAW = 'DRAW'
    SCATTER = 'SCATTER'
    ## ORIGIN IS (x, y)
    ORIGIN = 'ORIGIN'
    IS = 'IS'
    ## ROT IS (X, Y)
    ROT = 'ROT'
    ## SCALE IS (X,Y)
    SCALE = 'SCALE'
    ## IN [COLOR_NAME]
    IN = 'IN'
    COLOR = 'COLOR'
    ## LINE[LINE_WIDTH][LINE_TYPE]
    LINE = 'LINE'
    LINE_TYPE = 'LINE_TYPE'
    ## TITLE('USER_STRING')
    TITLE = 'TITLE'
    USER_STRING = 'USER_STRING'
    ## XLABEL('USER_STRING'), YLABEL('USER_STRING')
    XLABEL = 'XLABEL'
    YLABEL = 'YLABEL'
    ## GRID ON GRID OFF
    GRID = 'GRID'
    ON = 'ON'
    OFF = 'OFF'
    ## DYNAMIC ON DYNAMIC OFF
    DYNAMIC = 'DYNAMIC'
    # 分隔符
    SEMICO = ";"
    L_BRACKET = "("
    R_BRACKET = ")"
    COMMA = ","
    # 运算符
    PLUS = '+'
    MINUS = '-'
    MUL = '*'
    DIV = '/'
    MOD = '%'
    POWER = '**'
    # 空记号 源程序结束
    NONTOKEN = 'NULL'
    # 出错符号，非法输入
    ERRORTOKEN = 'ERROR'


class Tokens:
    # 构造函数，默认情况下值为0，函数指针指向空
    def __init__(self, tokenType, lexeme, value = 0.0, function = None):
        self.tokenType = tokenType
        self.lexeme = lexeme
        self.value = value
        self.function = function
    # 展示token的内容，便于调试
    def disp(self):
        # print('{:<10s}|{:x<10s}|{:<10f}|{}'.format(self.tokenType.name,  self.lexeme, self.value, self.function))
        print('{:<30s}|{:<28s}\t|{:<10f}|{}'.format(self.tokenType.name,  self.lexeme, self.value, self.function))

TokenTable = {
    # 常量：COUST_ID
    "PI" : Tokens(TokenType.CONST_ID, "PI", 3.1415926, None),
    "E"  : Tokens(TokenType.CONST_ID,"E",2.71828,None),
    # 唯一变量T ：T
    "T"  : Tokens(TokenType.T, "T"),
    # 函数
    "SIN": Tokens(TokenType.FUNC, "SIN", 0, np.sin),
    "COS": Tokens(TokenType.FUNC, "COS", 0, np.cos),
    "TAN": Tokens(TokenType.FUNC, "TAN", 0, np.tan),
    "SQRT": Tokens(TokenType.FUNC, "SQRT", 0, np.sqrt),
    "EXP": Tokens(TokenType.FUNC, "EXP", 0, np.exp),
    "LN": Tokens(TokenType.FUNC, "LN", 0, np.log),
    # 保留关键字
    "ORIGIN":Tokens(TokenType.ORIGIN, "ORIGIN"),
    "IS":Tokens(TokenType.IS, "IS"),
    "ROT":Tokens(TokenType.ROT, "ROT"),
    "SCALE":Tokens(TokenType.SCALE, "SCALE"),
    "FOR":Tokens(TokenType.FOR, "FOR"),
    "FROM":Tokens(TokenType.FROM, "FROM"),
    "TO":Tokens(TokenType.TO, "TO"),
    "STEP":Tokens(TokenType.STEP,"STEP"),
    "DRAW":Tokens(TokenType.DRAW, "DRAW"),
    "SCATTER":Tokens(TokenType.SCATTER,"SCATTER"),
    "COLOR":Tokens(TokenType.COLOR, "COLOR"),
    "LINE":Tokens(TokenType.LINE,"LINE"),
    "DOTS":Tokens(TokenType.LINE_TYPE,"DOTS"),
    "DASHED":Tokens(TokenType.LINE_TYPE,"DASHED"),
    "SOLID":Tokens(TokenType.LINE_TYPE,"SOLID"),
    "XLABEL":Tokens(TokenType.XLABEL,"XLABEL"),
    "YLABEL":Tokens(TokenType.YLABEL,"YLABEL"),
    "TITLE":Tokens(TokenType.TITLE,"TITLE"),
    "GRID":Tokens(TokenType.GRID, "GRID"),
    "ON":Tokens(TokenType.ON, "ON"),
    "OFF":Tokens(TokenType.OFF, "OFF"),
    "DYNAMIC":Tokens(TokenType.DYNAMIC, "DYNAMIC"),
    # 颜色
    "DIMGRAY":Tokens(TokenType.COLOR,"DIMGRAY"),
    "GOLDENROD":Tokens(TokenType.COLOR,"GOLDENROD"),
    "KHAKI":Tokens(TokenType.COLOR,"KHAKI"),
    "DARKSEAGREEN":Tokens(TokenType.COLOR,"DARKSEAGREEN"),
    "DARKKHAKI":Tokens(TokenType.COLOR,"DARKKHAKI"),
    "PINK":Tokens(TokenType.COLOR,"PINK"),
    "LIGHTSALMON":Tokens(TokenType.COLOR,"LIGHTSALMON"),
    "LIGHTCORAL":Tokens(TokenType.COLOR,"LIGHTCORAL"),
    "STEELBLUE":Tokens(TokenType.COLOR,"STEELBLUE"),
    "LIGHTSKYBLUE":Tokens(TokenType.COLOR,"LIGHTSKYBLUE"),
    "IN":Tokens(TokenType.IN, "IN")
}